import { defineStore } from "pinia";
import {ref} from "vue";

export const useAddressStore = defineStore('address', () => {
    const address = ref([
      {
        addressId : "",
        receiveName : "",
        receivePhone : "",
        province : "",
        provinceCode : "",
        area : "",
        areaCode : "",
        city : "",
        cityCode : "",
        street : "",
        postalCode : "",
      }
    ])

    // 当前选中地址索引
    const curIndex = ref(0)

    const setCurIndex = (index) => {
      curIndex.value = index
    }

    const getCurIndex = () => {
      return curIndex.value
    }

    // 获取地址列表
    const getAddressList = () => {
      return address.value
    }

    // 添加地址
    const addAddress = (data) => {
      address.value.push(data)
    }

    const setAddressList = (data) => {
      address.value = data
    }

    const getAddressInfo = () => {
      return address.value[curIndex.value]
    }


    return {
        setCurIndex,
        getCurIndex,
        getAddressList,
        getAddressInfo,
        addAddress,
        setAddressList
    }
}, {
  persist: true
})
